package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import com.itheima.utils.PojoDataExchange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 17:10
 * @Desc:
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    @Override
    public Page<Setmeal> listAll(Integer page, Integer pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);

        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", "%".concat(name).concat("%"));
        }
        wrapper.orderByDesc("update_time");

        return setmealMapper.selectPage(page1, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            int i = setmealMapper.updateById(setmeal);

            if (i == 0)
                i = 1 / 0;
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSetmeal(Long[] ids) {
        for (Long id : ids) {
            int i = setmealMapper.deleteById(id);

            if (i == 0)
                i = 1 / 0;
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSetmeal(SetmealNew setmealNew) {
        Setmeal setmeal = PojoDataExchange.backExchange(setmealNew);

        int i = setmealMapper.insert(setmeal);

        if (i != 0) {
            Page<Setmeal> setmealPage = listAll(1, 10, setmealNew.getName());
            setmealNew.setId(setmealPage.getRecords().get(0).getId());

            setmealDishService.addSetmealDish(setmealNew);

            return 1;
        }

        return 0;
    }

    @Override
    public List<Setmeal> findSetmeatlByCategoryId(Long id) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", id);

        return setmealMapper.selectList(wrapper);
    }

    @Override
    public SetmealNew findSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishService.findSetmeatlDishByDishId(id);

        SetmealNew setmealNew = PojoDataExchange.dataExchange(setmeal);
        setmealNew.setSetmealDishes(setmealDishes);

        return setmealNew;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSetmealInfo(SetmealNew setmealNew) {
        Setmeal setmeal = PojoDataExchange.backExchange(setmealNew);

        int i = setmealMapper.updateById(setmeal);
        if (i == 0) {
            i = 1 / 0;
        } else {
            i = setmealDishService.deleteBySetmealId(setmeal.getId());
            if (1 == 0) {
                i = 1 / 0;
            } else
                setmealDishService.addSetmealDish(setmealNew);
        }

        return 1;
    }

    @Override
    public List<Setmeal> findSetmeal(Long categoryId, Integer status) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        if (status != null)
            wrapper.eq("status", status);

        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<Dish> findDishBySetmealId(Long id) {
        List<SetmealDish> setmealDishes = setmealDishService.findBySetmealId(id);

        ArrayList<Dish> dishes = new ArrayList<>();
        if (setmealDishes.size() != 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                DishNew dishNew = dishService.findDishById(setmealDish.getDishId());
                Dish dish = PojoDataExchange.backExchange(dishNew);

                dishes.add(dish);
            }
        } else {
            Dish dish = PojoDataExchange.backExchange(dishService.findDishById(id));
            dishes.add(dish);
        }

        return dishes;
    }
}
